﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using HelperUnity;

namespace IYASAKANet_CDDY
{
    public class NetCommon
    {

        /// <summary>
        /// 得到当前动态库的路径.
        /// </summary>
        /// <returns></returns>
        public static string GetDllPath()
        {
            string str = Assembly.GetExecutingAssembly().CodeBase;
            int start = 8;// 去除file:///
            int end = str.LastIndexOf('/');// 去除文件名xxx.dll及文件名前的/
            str = str.Substring(start, end - start);
            return str;
        }

        /// <summary>
        /// 得到错误的结果.
        /// </summary>
        /// <param name="strFailMessage"></param>
        /// <returns></returns>
        public static string GetFailResult(string strFailMessage)
        {

            return "{\"isSuccess\":\"0\",\"message\": \"" + strFailMessage + "\",\"vehicleData\": {}}";
        }

        /// <summary>
        /// 得到正确的结果.
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string GetCorrectResult(Dictionary<string, string> dic)
        {
            string strResult = string.Empty;
            try
            {
                strResult = "{\"isSuccess\":\"1\",\"message\": \"成功\",\"vehicleData\":{";
                var vKeys = dic.Keys.ToList();
                for (int i = 0; i < vKeys.Count; i++)
                {
                    if (i != vKeys.Count - 1)
                    {
                        strResult += "\"" + vKeys[i] + "\":" + "\"" + dic[vKeys[i]].ToString() + "\",";
                    }
                    else
                    {
                        strResult += "\"" + vKeys[i] + "\":" + "\"" + dic[vKeys[i]].ToString() + "\"";
                    }
                }
                strResult += "}}";
            }
            catch (Exception ex)
            {
                strResult = GetFailResult(ex.Message);
            }

            return strResult;
        }

        /// <summary>
        /// 得到正确的结果.
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string GetNissanReturnJson(Dictionary<string, string> dic)
        {
            string strResult = string.Empty;
            try
            {
                strResult = string.Empty;
                var vKeys = dic.Keys.ToList();
                for (int i = 0; i < vKeys.Count; i++)
                {
                    if (i != vKeys.Count - 1)
                    {
                        strResult += "[\"" + vKeys[i] + "\"," + "\"" + dic[vKeys[i]].ToString() + "\"],";
                    }
                    else
                    {
                        strResult += "[\"" + vKeys[i] + "\"," + "\"" + dic[vKeys[i]].ToString() + "\"]";
                    }
                }
            }
            catch (Exception ex)
            {
                strResult = GetFailResult(ex.Message);
            }

            return strResult;
        }

        /// <summary>
        /// 得到正确的结果.
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string GetCorrectResult()
        {
            string strResult = string.Empty;
            try
            {
                strResult = "{\"isSuccess\":\"1\",\"message\": \"成功\"}";
            }
            catch (Exception ex)
            {
                strResult = GetFailResult(ex.Message);
            }

            return strResult;
        }

        /// <summary>
        /// 记录Vee日志.
        /// </summary>
        /// <param name="strLog">日志内容.</param>
        public static void WriteLog(string strLogPath, string strLog)
        {
            // 文件流
            FileStream fs = null;

            // 数据流.
            StreamWriter sw = null;

            try
            {
                if (Directory.Exists(strLogPath) == false)
                {
                    Directory.CreateDirectory(strLogPath);
                }

                if (fs == null)
                {
                    fs = new FileStream(strLogPath + "/Log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                }

                if (sw == null)
                {
                    sw = new StreamWriter(fs, Encoding.Default);
                }

                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strLog);
                sw.Close();
                fs.Close();
            }
            catch
            {
                if (sw != null)
                {
                    sw.Dispose();
                }

                if (fs != null)
                {
                    fs.Dispose();
                }

                fs = null;
                sw = null;
            }
        }

        /// <summary>
        /// 得到Json值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetJsonValue(object obj)
        {
            string strRet = "";
            try
            {
                if (obj != null)
                {
                    strRet = obj.ToString().Trim();
                }
                else
                {
                    strRet = "";
                }
            }
            catch (Exception ex)
            {
                strRet = "";
            }

            return strRet;
        }


        /// <summary>
        /// 得到动态配置文件.
        /// </summary>
        /// <returns></returns>
        public static string[] GetAppConfig()
        {
            string[] strAry = new string[6];
            try
            {
                string strPath = GetDllPath() + "//DllConfig.ini";
                IniFileOperation intFile = new IniFileOperation(strPath);

                strAry[0] = intFile.IniReadValue("Setup", "DBPathMes");
                strAry[1] = intFile.IniReadValue("Setup", "DBPathU9");
                strAry[2] = intFile.IniReadValue("Setup", "ReadUrlPath");
                strAry[3] = intFile.IniReadValue("Setup", "ReadClassName");
                strAry[4] = intFile.IniReadValue("Setup", "ReadMethodName");
            }
            catch (Exception ex)
            {

            }

            return strAry;
        }
    }
}
